package com.the4man.framework.utils;

import java.util.Arrays;

/**
 * Created by lenovo on 2017/12/12.
 */
public class TestMain {
    public TestMain() {
    }

    public static void main1(String[] args) {
        byte[] bs = hexStr2Bytes("d6");
        System.out.println(bs);
        byte[] bs1 = new byte[]{12, 12, 8, 3};
        System.out.println("bs:" + byte2HexStr(bs1));
    }

    public static void main(String[] args) {
        String hexStr = "0c0c0803000000000d0800000001050400000007000000000c0c0803000000000d060000000002080d0406080b0a00050005000600000000030a0509000a090d0f090c0200020001000200000000000000000000000000020b040a07000000030000000000000000000000000000000000000000000000000c0c0803000000000d060000000002080d0406080b0a000500050006000000000a04030e0103070b0a05040200020001000200000000000000000000000000020b040a07000000020000000000000000000000000000000000000000000000000c0c0803000000000d060000000002080d0406080b0a00050005000600000000030409090f080b06060f050900020001000200000000000000000000000000020b050a07000000020000000000000000000000000000000000000000000000000c0c0803000000000d060000000002080d0406080b0a000500050006000000000306050f0c0f0e0d01040f0200020001000200000000000000000000000000020b070a070000000200000000000000000000000000000000000000000000000000000a0b0c0c0c0803000000000d0800000001050400000007000000000c0c0803000000000d060000000002080d0406080b0a00050005000600000000030a0509000a090d0f090c0200020001000200000000000000000000000000020b040a07000000030000000000000000000000000000000000000000000000000c0c0803000000000d060000000002080d0406080b0a000500050006000000000a04030e0103070b0a05040200020001000200000000000000000000000000020b040a07000000020000000000000000000000000000000000000000000000000c0c0803000000000d060000000002080d0406080b0a00050005000600000000030409090f080b06060f050900020001000200000000000000000000000000020b050a07000000020000000000000000000000000000000000000000000000000c0c0803000000000d060000000002080d0406080b0a000500050006000000000306050f0c0f0e0d01040f0200020001000200000000000000000000000000020b070a070000000200000000000000000000000000000000000000000000000000000";
        byte[] tmp = hexStr2Bytes(hexStr);
//        byte[] tmp = null;
//        byte[] left = null;
//        byte[] tmp = Arrays.copyOf(bs, bs.length);
        String apMac = null;
        String eqMac = null;

        for(int i = 0; i < tmp.length; ++i) {
            if(tmp.length - i < 96) {
                byte[] left = new byte[tmp.length - i];

                for(int j = 0; j < left.length; ++j) {
                    left[j] = tmp[i + j];
                }

                System.out.println("left bytes:" + byte2HexStr(left));
                break;
            }

            if(tmp[i] == 12 && tmp[i + 1] == 12 && tmp[i + 2] == 8 && tmp[i + 8] == 13 && tmp[i + 9] == 6) {
                byte[] apMac_bs = new byte[12];
                byte[] eqMac_bs = new byte[12];

                for(int j = 0; j < 12; ++j) {
                    apMac_bs[j] = tmp[i + 16 + j];
                    eqMac_bs[j] = tmp[i + 32 + j];
                }

                apMac = byte2HexStr(apMac_bs);
                eqMac = byte2HexStr(eqMac_bs);
                System.out.println("apmac:" + apMac);
                System.out.println("eqmac:" + eqMac);
                i += 95;
            }
        }

    }

    public static byte[] toStringHex(String s) {
        byte[] baKeyword = new byte[s.length() / 2];

        for(int i = 0; i < baKeyword.length; ++i) {
            try {
                baKeyword[i] = (byte)(255 & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

        return baKeyword;
    }

    public static String byte2HexStr(byte[] b) {
        String hs = "";
        String stmp = "";

        for(int n = 0; n < b.length; ++n) {
            stmp = Integer.toHexString(b[n] & 255);
            if(stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }

        return hs.toUpperCase();
    }

    public static byte[] hexStr2Bytes(String src) {
//        int m = false;
//        int n = false;
        int l = src.length() / 2;
        byte[] ret = new byte[l];

        for(int i = 0; i < l; ++i) {
            int m = i * 2 + 1;
            int n = m + 1;
            ret[i] = uniteBytes(src.substring(i * 2, m), src.substring(m, n));
        }

        return ret;
    }

    private static byte uniteBytes(String src0, String src1) {
        byte b0 = Byte.decode("0x" + src0).byteValue();
        b0 = (byte)(b0 << 4);
        byte b1 = Byte.decode("0x" + src1).byteValue();
        byte ret = (byte)(b0 | b1);
        return ret;
    }
}
